using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Comteck.Dto.Orders;
using Comteck.Ktt.Services.Orders;
using Comteck.Linq;
using Comteck.Localization;
using Comteck.Winforms;
using Comteck.Winforms.Data;
using Comteck.Winforms.Forms;
using Comteck.Winforms.Infrastructure;
using DevExpress.XtraBars;
using ExpressMapper.Extensions;

namespace Comteck.Ktt.WinUI.Orders {
  /// <summary>
  /// 自提点选择
  /// </summary>
  public partial class FrmSelfOrgDialog : FrmDialogBase {
    private readonly ISelfOrgService selfOrgService;
    private CKObservableCollection<SelfOrgDto> mainDataList;

    /// <summary>
    /// 查询条件
    /// </summary>
    [Browsable(false)]
    public Action<SelfOrgQuery> QueryAction { get; set; }

    /// <summary>
    /// 查询模型
    /// </summary>
    private SelfOrgQuery queryModel = new SelfOrgQuery();

    /// <summary>
    /// 确定的回调方法
    /// </summary>
    [Browsable(false)]
    public BoolAction<List<SelfOrgDto>> ImportAction { get; set; }

    /// <summary>
    /// 确定的回调方法
    /// </summary>
    [Browsable(false)]
    public BoolActionAsync<List<SelfOrgDto>> ImportActionAsync { get; set; }

    public FrmSelfOrgDialog(ISelfOrgService selfOrgService) {
      this.selfOrgService = selfOrgService;
      InitializeComponent();
    }

    private async void FrmSelfOrgDialog_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }
      // 设置单选还是多选
      this.SetGridViewSelectMode(gvMain);
      
      this.QueryAction?.Invoke(queryModel);
      gcMain.DataSource = mainDataList = new CKObservableCollection<SelfOrgDto>(this);
      colCreateTime.FormatDate();

      // 自提点标签
      var selfOrgTagDtoList = await ServerDbDataHelper.GetAllSelfOrgTagAsync();
      await BindHelper.BindSelfOrgTag(searchTagId, null, selfOrgTagDtoList);
      await BindHelper.BindSelfOrgTag(rilpTagIds, null, selfOrgTagDtoList);

      // 默认查询
      btnSearch.PerformClick();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnSearch_ItemClick(object sender, ItemClickEventArgs e) {
      // search model
      queryModel.Name = searchName.EditValue.To<string>();
      queryModel.Contacts = searchContacts.EditValue.To<string>();
      queryModel.Phone = searchPhone.EditValue.To<string>();
      queryModel.TagId = searchTagId.EditValue.To<int?>();

      // search
      ajaxLoad.Show();
      var result = await selfOrgService.GetListAsync(queryModel);
      ajaxLoad.Hide();

      result.ToList().MapTo(mainDataList, x => x.Id, x => x.Id);
    }

    private async void btnImport_ItemClick(object sender, ItemClickEventArgs e) {
      // get rows
      var result = gvMain.GetSelectedRowsList<SelfOrgDto>();

      if (this.SelectType == EnumSelectType.Single && result.Count > 1) {
        this.ShowMsg(BaseResources.Dialog_Choose_One);
        return;
      } else if (result.IsNullOrEmpty()) {
        return;
      }

      // import
      if (this.ImportAction != null) {
        if (this.ImportAction?.Invoke(result) == true) {
          this.DialogResult = DialogResult.OK;
        }
      } else if (this.ImportActionAsync != null) {
        if ((await this.ImportActionAsync(result)) == true) {
          this.DialogResult = DialogResult.OK;
        }
      } else {
        this.ShowMsg("请注册导入事件");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void gvMain_DoubleClick(object sender, EventArgs e) {
      if (!(gvMain.GetFocusedRow() is SelfOrgDto model)) {
        return;
      }

      if (this.ImportAction == null && this.ImportActionAsync == null) {
        this.ShowMsg("请注册导入事件");
        return;
      }

      var result = new List<SelfOrgDto>() { model };

      // import
      if (this.ImportAction != null) {
        if (this.ImportAction?.Invoke(result) == true) {
          this.DialogResult = DialogResult.OK;
        }
      } else if (this.ImportActionAsync != null) {
        if ((await this.ImportActionAsync(result)) == true) {
          this.DialogResult = DialogResult.OK;
        }
      } else {
        this.ShowMsg("请注册导入事件");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }
  }
}
